Ext.define('QMS.view.Unit', {
	extend: 'Ext.form.Panel',
	xtype: 'Unit-view',

	frame: true,
	title: 'Unit',
	bodyPadding: 10,
	autoScroll: true,
	flex: 1,

	fieldDefaults: {
		labelAlign: 'right',
		labelWidth: 150,
		msgTarget: 'side'
	},

	items: [{
		xtype: 'fieldset',
		title: 'Information',
		defaultType: 'textfield',
		defaults: {
			anchor: '100%'
		},
		items: [
			{ name: 'id', xtype: 'hiddenfield'},
			{ allowBlank:false, fieldLabel: 'Unit Name', name: 'name' },
			{ fieldLabel: 'Unit Description', name: 'description', xtype: 'textareafield' },
		]
	}],

	buttons: [{
		text: 'Save',
		formBind: true,
		handler: function() {
			var form = this.up('form').getForm(), // get the basic form
				record = form.getRecord(); // get the underlying model instance
			if (form.isValid()) { // make sure the form contains valid data before submitting
				if (record != undefined) {
					form.updateRecord(record); // update the record with the form data
					record.save({ // save the record to the server
						success: function(r) {
							QMS.app.fireEvent("treeelementchanged");
						},
						failure: function(r) {
							Ext.Msg.alert('Failure', 'Failed to save unit.');
						}
					});
				} else {
					record = Ext.create('QMS.model.Unit');
					record.set(form.getValues());
					QMS.app.fireEvent("unitformsubmitted", record);
				}
			} else { // display error alert if the data is invalid
				Ext.Msg.alert('Invalid Data', 'Please correct form errors.');
			}
		}
	}]
});
